package floappcontest.core.backbeans;

import java.util.ArrayList;
import java.util.List;


import floappcontest.core.beans.Contest;
import floappcontest.core.beans.Faq;
import floappcontest.core.control.Control;
import floappcontest.core.dao.factory.HibernateDAOFactory;
import floappcontest.core.exception.FloAppException;

public class FaqBean {

	private static final String OK = "OK"; //$NON-NLS-1$
	
	private static final String FAILED = "FAILED"; //$NON-NLS-1$
	
	private Control control;
	
	private Faq faq;
	
	private List<Contest> contestList;
	
	private List<Faq> faqList; 
	
	private Contest contest;
	

	public FaqBean(){		
		setFaq( new Faq() );
		setControl( new Control( new HibernateDAOFactory() ) );
		setContestList( new ArrayList<Contest>());
		contest = new Contest();
		faqList = new ArrayList<Faq>();
	}
	
	public Faq getFaq() {
		return this.faq;
	}

	public void setFaq(Faq faq) {
		this.faq = faq;
	}

	private Control getControl() {
		return this.control;
	}

	private void setControl(Control control) {
		this.control = control;
	}
	
	public String insertFaq(){
		try{
			getControl().insertFaq(this.faq);
			setFaq( new Faq());
			return OK;
		} catch (FloAppException e){
			setFaq(new Faq());
			return FAILED;
		}
	}

	public List<Contest> getContestList() {
		this.contestList = getControl().listContests();
		return this.contestList;
	}

	public void setContestList(List<Contest> contestList) {
		this.contestList = contestList;
	}

	public String getContestName(){
		return getFaq().getContest().getName();
	}
	
	public void setContestName(String newContestName){
		this.faq.getContest().setName(newContestName);
		faqList = getControl().listFaqByContestName(newContestName);
	}
	
	public String getQuestion(){
		return getFaq().getQuestion();
	}
	
	public void setQuestion(String newQuestion){
		getFaq().setQuestion(newQuestion);
	}
	
	public String getAnswer(){
		return getFaq().getAnswer();
	}
	
	public void setAnswer(String answer){
		getFaq().setAnswer(answer);
	}

	public Contest getContest() {
		return contest;
	}

	public void setContest(Contest contest) {
		this.faq.setContest(contest);
		this.contest = contest;
	}
	
	public String listByContestName(){
		if (contest.getName().equals("")){
			return "FAILED";
		}
		return "OK";
	}

	public List<Faq> getFaqList() {
		return faqList;
	}

	public void setFaqList(List<Faq> faqList) {
		this.faqList = faqList;
	}
	
}